<template>
	<view class="_segmented">
		<uni-segmented-control :current="current" :values="items" styleType="text" active-color="#4cd964" @clickItem="onClickItem" />
	</view>
	<view class="content">
		<view v-if="current === 0">
			<view class="_content1">
					
					<uni-forms :modelValue="fromData" :label-position="`top`">
						<uni-forms-item label="角色模板">
							<uni-data-checkbox :wrap="true" mode="tag" v-model="rolseModel" :localdata="rolseList"></uni-data-checkbox>
						</uni-forms-item>
						<uni-forms-item label="助手呢称">
							<uni-easyinput></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item label="角色音色">
							<uni-data-select v-model="currentVoice" :localdata="voiceList" @change="viceChange" label="应用选择"></uni-data-select>
						</uni-forms-item>
					
						<uni-forms-item label="语言模型">
							<uni-data-select v-model="currentLanguageModel" :localdata="languageModelList" @change="languageModelChange" label="应用选择"></uni-data-select>
						</uni-forms-item>

					</uni-forms>
			
				
				<button class="_saveActive1" @click="saveSetting">保存</button>
			</view>
		</view>
		<view v-if="current === 1">
			<view class="_content1">
				<view class="" style="margin-bottom: 50rpx">
					<uni-segmented-control :current="currentMode" :values="modeList" styleType="button" active-color="#4cd964" @clickItem="onClickModeList" />
				</view>

				<view class="_content1_item" v-if="currentMode == 0">
					
				</view>
				<view class="_content1_item" v-if="currentMode == 1">
					<view class="">
						<uni-file-picker  :autoUpload="false"  @select="(val)=>select(val,'curListenImg')"  limit="1" fileMediatype="image" v-model="curListenImg" title="聆听图片上传(设备在聆听用户讲话时显示的图片)"></uni-file-picker>
					</view>
					<view class="" style="margin-top: 50rpx">
						<uni-file-picker :autoUpload="false" @select="(val)=>select(val,'curTalkImg')" limit="1" fileMediatype="image" v-model="curTalkImg" title="对话图片上传(设备在回复用户时显示的图片)"></uni-file-picker>
					</view>
					
				</view>
				<view class="_content1_item" v-if="currentMode == 2">
					<view class="">
						<uni-file-picker :autoUpload="false" limit="1" fileMediatype="video" @select="(val)=>select(val,'curListenVideo')" v-model="curListenVideo" title="聆听视频上传(设备在聆听用户讲话时显示的视频)"></uni-file-picker>
					</view>
					<view class="" style="margin-top: 50rpx">
						<uni-file-picker :autoUpload="false" limit="1" fileMediatype="video" @select="(val)=>select(val,'curTalkVideo')" v-model="curTalkVideo" title="对话视频上传(设备在回复用户时显示的视频)"></uni-file-picker>
					</view>
					
				</view>
				
					<button class="_saveActive1" @click="saveResUce">保存</button>
			
	
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import { uniCommon ,uniToast} from '../../utils';
import{ upload } from "../../utils/request.js"
import { uplodUrl } from "../../api"
import { adviceConfig ,updateAdvice , quaryvideo, quarypic,talktype,addpic,addvideo} from '../../api';
let current = ref(0);
let items = ['参数设置', '模式设置'];

// 回显 或 提交 数据
let fromData = ref({});

//选中角色模板
let rolseModel = ref(0);
// 角色模板list
let rolseList = ref([
	{ text: '老师', value: 0 },
	{ text: '医生', value: 1 },
	{ text: '我我队长', value: 2 },
	{ text: '好奇小男孩', value: 3 }
]);

// 当前模式
let currentMode = ref(0);
// 模式list
let modeList = ['表情模式', '图片模式', '视频模式'];
let detail = ref();
let currentLanguageModel = ref();
let languageModelList = ref([]);
let currentVoice = ref();
let voiceList = ref([]);

let curListenImg= ref([]);
let curTalkImg = ref([]);

let curListenVideo = ref([]);
let curTalkVideo = ref([]);

onLoad((option)=>{
	if(option.obj){
		detail.value = uniCommon.jsonToObject(option.obj) 
		console.log("设备详情：",detail.value)
		if(detail.value){
			currentLanguageModel.value = detail.value.modelId
			currentVoice.value = detail.value.roleId
			currentMode.value = detail.value.talkType
		}
		getAdviceDetail(detail.value.deviceId)
		getquaryvideo(detail.value.deviceId)
		getquarypic(detail.value.deviceId)
	}
	
})
async function savetalktype(){
	await addpic({
		deviceId:detail.value.deviceId,
		listenPicName:curListenImg.value[0]?.name || '',
		listenPicUrl:curListenImg.value[0]?.url || '',
		talkPicName:curTalkImg.value[0]?.name || '',
		talkPicUrl:curTalkImg.value[0]?.url || '',
	})
	await addvideo({
		deviceId:detail.value.deviceId,
		listenVideoName:curListenVideo.value[0]?.name || '',
		listenVideoUrl:curListenVideo.value[0]?.url || '',
		talkVideoName:curTalkVideo.value[0]?.name || '',
		talkVideoUrl:curTalkVideo.value[0]?.url || '',
	})
   let 	res = await talktype({deviceId:detail.value.deviceId,talkType:currentMode.value})
	if(res.code == 200){
		uniToast.toast("保存成功")
	}
}
//获取当前模式 视频
async function getquaryvideo(deviceId){
	try{
		let res  = await quaryvideo({deviceId})
		// 回显视频
		if(res.code == 200){
			curListenVideo.value = [{name:res.data.listenVideoName,extname:'curListenVideo',url:res.data.listenPicUrl}]
			curTalkVideo.value = [{name:res.data.talkVideoName,extname:'curTalkVideo',url:res.data.talkPicUrl}]
		}
		
	}catch(err){}
	
}
// 获取当前模式 图片
async function getquarypic(deviceId){
	try{
		let res = await quarypic({deviceId})
		if(res.code == 200){
			 curListenImg.value= [{name:res.data.listenPicName,extname:'curListenImg',url:res.data.listenPicUrl}];
			 curTalkImg.value = [{name:res.data.talkPicName,extname:'curTalkImg',url:res.data.talkPicUrl}];
		}
	}catch(err){}
	
	// 回显图片
	
}
async function select(val,type){
	console.log(val,type)
	try{
		console.log(123)
		let res = await upload({url:uplodUrl,file:val.tempFiles[0].url})
		console.log(res)
		res = JSON.parse(res.data)
		if(res.code ==200){
			console.log(res)
			let tempList=[{name:res.data.filename,extname:type,url:res.data.url}];
			switch(type){
				case 'curListenImg': 	
				curListenImg.value= tempList;
				break;
				case 'curTalkImg':
				curTalkImg.value = tempList;
				break;
				case 'curListenVideo':
				curListenVideo.value = tempList;
				break;
				case 'curTalkVideo':
				curTalkVideo.value = tempList;
				break;
			}
			
			
		}
		
	}catch(err){
		uniToast.toast("上传失败")
	}
	
}

// 保存模式设置
function saveResUce(){
	console.log(curListenVideo.value)
	console.log(curTalkVideo.value)
	console.log(curListenImg.value)
	console.log(curTalkImg.value)
}
// 设备config 字典
async function getAdviceDetail(deviceId){
		let res = await adviceConfig({deviceId})
		
		if(res.code ==200){
			languageModelList.value = res.data.llmConfigs.map(item=>{
				return {
					...item,
					text:`${item.configName}(${item.configDesc})` ,
					value:item.configId
				}
			})
			
			voiceList.value = res.data.voiceConfigs.map(item=>{
				return {
					...item,
					text:item.roleName,
					value:item.roleId,
				}
			}
			)
		}
}
function viceChange(val){
	console.log(val)
	let objVice = voiceList.value.find(item=>item.value == val)
	if(objVice){
		detail.value.roleId =objVice.objVice;
		detail.value.roleName =objVice.roleName
	}
	
	
}
function languageModelChange(val){
	console.log(val)
	let obj = languageModelList.value.find(item=>item.value == val)
	console.log(obj)
	if(obj){
		// detail.value.modelId = obj.configId
		detail.value.modelId = obj.configId
		detail.value.configName = obj.configName
		detail.value.configDesc = obj.configDesc
		
	}
}
function onClickItem(e) {
	if (current.value !== e.currentIndex) {
		current.value = e.currentIndex;
	}
}
function onClickModeList(e) {
	if (currentMode.value !== e.currentIndex) {
		currentMode.value = e.currentIndex;
	}
}

async function saveSetting(){
	console.log("提交",detail.value)
	
	let params = {
		...detail.value
	}
	try{
		let res = await updateAdvice(params)
		if(res.code == 200){
			uniToast.toast("操作成功")
		}
	}catch(err){
		uniToast.toast("操作失败，请稍后再试")
	}
	
}
</script>

<style lang="scss">
._segmented {
	padding: 20rpx 30rpx;
	background: #fff;
	margin-top: 20rpx;
}
._content1 {
	background: #fff;
	margin: 20rpx 20rpx;
	padding: 40rpx 20rpx;
	position: relative;
	min-height: 1000rpx;
	display: flex;
	flex-direction: column;
	
	
	
}
._saveActive1 {
	width: 400rpx;
	background: #2979ff;
	color: #fff;
	
	margin-top: auto; 
	// position: absolute;
	// bottom:20rpx;
	// left:50%;
	// transform: translate(-50%,-50%);
	&::after {
		content: '';
		background: transparent;
		border: none;
	}
}
// ._content1_item {
// 	margin: 20rpx;
// 	margin-top: -20rpx;
// 	background: #fff;
// 	padding: 0 20rpx;
	
// }
</style>
